package com.utils;

import com.alibaba.fastjson.JSON;

public class R {

    /**
     * 状态码 0 表示成功
     */

    private Integer code;
    /**
     * 数据
     */
    private Object data;
    /**
     * 描述
     */
    private String msg;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public R() {

    }

    public R(Integer code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }


    /**
     * 成功，不传入数据
     * @return
     */
    public static String ok() {
        return JSON.toJSONString(new R(0, null, null));
    }

    /**
     *  成功，传入数据
     * @param data
     * @return
     */
    public static String ok(Object data) {
        return JSON.toJSONString(new R(0, data, null));
    }

    /**
     * 失败，传入描述信息
     * @param msg
     * @return
     */
    public static String fail(String msg) {
        return JSON.toJSONString(new R(-1, null, msg));
    }


    /**
     * 自定义状态码和错误信息
     * @param code
     * @param msg
     * @return
     */
    public static R buildCodeAndMsg(int code, String msg) {
        return new R(code, null, msg);
    }

}